package com.doit.kafkaday01;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.StringSerializer;

import java.util.Properties;

public class _01_kafka的生产者 {
    public static void main(String[] args) {
        Properties props = new Properties();
        //kafka必须要配置的参数
        props.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,"linux01:9092");
        props.setProperty(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        props.setProperty(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        //创建对象
        KafkaProducer<String, String> producer = new KafkaProducer<>(props);

        //用对象调用api
        producer.send(new ProducerRecord<String,String>("test","this message from java"));

        producer.flush();

        //关闭资源
        producer.close();


    }
}
